package com.hotel.adminuser.service;


import java.util.UUID;


import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hotel.adminuser.mapper.AdminUserMapper;
import com.hotel.adminuser.pojo.AdminUser;

import com.jt.common.utils.MapperUtil;

import redis.clients.jedis.JedisCluster;

@Service
public class AdminUserService {
	@Autowired
	private AdminUserMapper adminUserMapper;
	@Autowired
	private JedisCluster jedis;
	public String doLook(String username,String password) {
		//依然要进行user校验工作
		String password2=DigestUtils.md5Hex(password);
		AdminUser exist=adminUserMapper
				.selectUserByAdmainNameAndPassword(username,password2);
		if(exist==null){//登录失败
			return "";
		}else{//要引入顶替的逻辑
			//准备2个key值
			try{                
			String userLoginIdent=
					"user_login_"+exist.getId();
			String ticket="EM_TICKET"+
					System.currentTimeMillis()+exist.getId();
			String userJson=
					MapperUtil.MP.writeValueAsString(exist);
				if(jedis.exists(userLoginIdent)){
					//删除上次登录的ticket并且删除
					jedis.del(jedis.get(userLoginIdent));
				}
				//设置登录顶替相关的2个key-数据value
				jedis.setex(userLoginIdent, 60*60*3, ticket);
				jedis.setex(ticket, 60*60*2, userJson);
				return ticket;
			}catch(Exception e){
				e.printStackTrace();
				return "";
			}
		}
	}

	
}



